package com.old.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.old.dto.UserDTO;
import com.old.dto.UserUpdateInfoDTO;
import com.old.entity.User;
import com.old.mapper.UserMapper;
import com.old.result.PageBean;
import com.old.service.UserService;
import com.old.util.Md5Util;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
@RequiredArgsConstructor
public class UserServiceImpl implements UserService {

    private final UserMapper userMapper;

    @Override
    public User findByAccount(Integer account) {
        return userMapper.findByAccount(account);
    }

    @Override
    public void register(UserDTO user) {
        String password = user.getPassword();
        user.setPassword(Md5Util.getMD5String(password));
        user.setAvatar("https://cube.elemecdn.com/3/7c/3ea6beec64369c2642b92c6726f1epng.png");
        userMapper.register(user);
    }


    @Override
    public User findByAccountAndName(Integer account, String name) {
        return userMapper.findByAccountAndName(account, name);
    }

    @Override
    public void save(User user) {
        userMapper.save(user);
    }

    @Override
    public User findByID(Integer userId) {
        return userMapper.findByID(userId);
    }


    @Override
    public void updateUserInfo(Integer userId, UserUpdateInfoDTO userUpdateInfoDTO) {
        if (userId == null){
            userId = userUpdateInfoDTO.getUserId();
        }
        // 1. 查询当前用户
        User user = userMapper.findByID(userId);
        if (user == null) {
            throw new RuntimeException("用户不存在");
        }

        // 2. 判断密码是否重复(前端传入普通正常-前端传入原md5加密-前端传入普通相同)
        if (!userUpdateInfoDTO.getPassword().equals(user.getPassword())) {
            //不重复再更新密码
            user.setPassword(Md5Util.getMD5String(userUpdateInfoDTO.getPassword())); // 密码加密
        }
        // 2. 更新用户信息
        user.setAvatar(userUpdateInfoDTO.getAvatar());
        user.setName(userUpdateInfoDTO.getName());
        user.setUpdateTime(LocalDateTime.now());
        user.setAccount(userUpdateInfoDTO.getAccount());

        // 3. 保存到数据库
        userMapper.save(user);
    }

    @Override
    public void updateOnlineStatusToTrue(Integer id) {
        userMapper.updateOnlineStatustoTrue(id);
    }

    @Override
    public void updateOnlineStatusToFalse(int id) {
        userMapper.updateOnlineStatustoFalse(id);
    }

    @Override
    public void deleteByids(List<Integer> ids) {
        userMapper.deleteByids(ids);
    }

    @Override
    public void deleteByUserId(Integer userId) {
        userMapper.deleteByUserId(userId);
    }


    @Override
    public Integer getOnline() {
        return userMapper.getOnline();
    }

    @Override
    public Integer getOffline() {
        return userMapper.getOffline();
    }

    @Override
    public PageBean getUserList(Integer pageNo, Integer pageSize, Integer sortBy, String searchTerms) {
        PageHelper.startPage(pageNo, pageSize);

        List<User> list = userMapper.getUserList(sortBy, searchTerms);
        Page<User> p = (Page<User>) list;
        PageBean pageBean = new PageBean(p.getTotal(), p.getResult());
        return pageBean;
    }
}


